package com.jv.webmvc.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class MyAdvice {
	@ResponseBody
	@ExceptionHandler(value = Exception.class)
	public Map errorHandler(Exception ex) {
		Map<String ,Object> map = new HashMap<>();
		map.put("code", 400);
		//判断异常的类型,返回不一样的返回值
		if(ex instanceof MyException){
			map.put("msg","这是自定义异常");
		}
		else if(ex instanceof Exception){
			map.put("msg","这是其他异常");
		}
		return map;
	}
}
